package io.wongxd.solution.compose.custom.dialog

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import kotlinx.coroutines.delay


@Composable
fun WDialog(
    modifier: Modifier = Modifier,
    visible: Boolean,
    durationMillis: Int = 0,
    position: Alignment = Alignment.TopCenter,
    decorationColor: Color = Color(0x85000000),
    bgColor: Color = Color.Transparent,
    bgShape: Shape = MaterialTheme.shapes.small,
    progressColor: Color = Color(0xFF96FFE7),
    progressBackgroundColor: Color = Color(0xFF27C5A1),
    showProgress: Boolean = false,
    outSideDismiss: Boolean = false,
    onContentTap: () -> Unit = {},
    onDismiss: () -> Unit = {},
    content: @Composable (() -> Unit)
) {
    var _duration by WState(value = durationMillis)
    var progress by WState(value = 0f)
    val progressAnimate by animateFloatAsState(targetValue = progress, animationSpec = tween(
        durationMillis = durationMillis,
        easing = LinearEasing
    ), finishedListener = {
        _duration = durationMillis
    })

    LaunchedEffect(visible) {
        if (visible && durationMillis > 0) {
            progress = 1f
            delay(_duration.toLong())
            onDismiss()
            // increase duration to set progress(0f) faster
            _duration = 1
            progress = 0f
        }
    }

    if (visible)
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(decorationColor)
                .zIndex(2f)
                .clickable {
                    if (outSideDismiss) onDismiss()
                },
            contentAlignment = position
        ) {

            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable(
                        onClick = onContentTap,
                        interactionSource = remember {
                            MutableInteractionSource()
                        },
                        indication = null
                    )
            ) {

                Column(
                    modifier
                        .fillMaxWidth()
                        .clip(bgShape)
                        .background(bgColor)
                        .padding(10.dp),
                    verticalArrangement = Arrangement.spacedBy(16.dp)
                ) {
                    content()
                    if (showProgress) {
                        LinearProgressIndicator(
                            modifier = Modifier
                                .fillMaxWidth()
                                .clip(CircleShape),
                            progress = progressAnimate,
                            color = progressColor,
                            backgroundColor = progressBackgroundColor
                        )
                    }
                }

            }
        }

}

@Composable
fun <T> WState(value: T) = remember {
    mutableStateOf(value)
}